<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    function formatTime(time){
      // time 时间戳、字符串、时间对象
     if(typeof time === 'undefined'){
       return '参数必传';
     }
     let type = typeof time;
     let timestamp = 0;
     switch(type){
      case "number" :
        timestamp = time;
      break;
      case 'string':
        timestamp = new Date(time).getTime();
      break;
      case 'object':
        timestamp = time.getTime();
      break;
      default : break;
     }
    //  获取当前时间戳
    let nowTimestamp = Date.now();
    let minus = nowTimestamp - timestamp;

    let params = {
      just:180000,
      hour:3600000,
      day:86400000,
      mon:2592000000,
      year:31104000000
    }
    let reStr ="";
    if(minus <= params.just ){
      reStr="刚刚";
    }else if(minus > params.just && minus <= params.hour ){
      reStr = parseInt(minus/1000/60)+"分钟以前";
    }else if(minus > params.hour && minus <= params.day){
      reStr = parseInt(minus/1000/60/60)+"小时以前";
    }else if(minus > params.day && minus <= params.mon){
      reStr = parseInt(minus/1000/60/60/24)+"天以前";
    }else if(minus > params.mon && minus <= params.year){
      reStr = parseInt(minus/1000/60/60/24/30)+"月以前";
    }else{
      reStr = parseInt(minus/1000/60/60/24/30/12)+"年以前";
    }
    return reStr;
     /* 
     根据传入的时间 得到一个时间戳
     和当前时间 时间戳 进行 比较 得到差值
     根据差值
     格式化
     3分钟以内  刚刚
     3-60分钟  xx分钟以前
     60-24小时  xxx小时以前
     24 - 30天  xxx天以前
     30  12月  xx月以前
     >一年   显示  xx年以前
      */
    }
   
   console.log(formatTime(new Date('2020-5-10')))
  </script>

</body>
</html>